<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/23/023
 * Time: 11:06
 */

namespace app\index\controller;


use think\Db;

class Money extends Common
{
    // 获取货币数据
    public function getMoneyInfo(){
        $power = db('jujian')->where('id', $this->id)->value("lb_id");
        if ($power != 2) returnError("非代理身份，无操作权限！");
        $daili = db('jujian')->where('id', $this->id)->field('bh,xingming,hb_zu_id')->find();
        if (empty($daili['hb_zu_id'])) {
            returnError("未找到汇率模板");
        } else {
            $beilv = db('huobi_zu_detail')
                ->alias('de')
                ->join('huobi hb', "hb.id = de.hb_id and hb.bz=1")
                ->join('huobi_zu zu', 'zu.id = de.hb_zu_id', 'LEFT')
                ->join('jujian_bian b', "b.hb_id = hb.id and b.yh_id={$this->id}" , 'LEFT')
                ->where('zu.id', $daili['hb_zu_id'])
                ->order('de.hb_id')
                ->field('de.*,hb.name as huobiname,b.zong_yongjin')
                ->select();
            if (!$beilv) {
                returnError("未找到倍率");
            }else {
                $daili = array_merge($daili,array_shift($beilv));
                returnSuccess([$daili,$beilv]);
            }
        }
    }

    // 货币转换
    public function saveMoney(){
        $power = db('jujian')->where('id', $this->id)->value("lb_id");
        if ($power != 2) returnError("非代理身份，无操作权限！");
        $rmbId = input('rmb_id/d');
        $wbId = input('wb_id/d');
        $huobi = input('hb_id/d');
        $money = input('money/d');
        if (!$rmbId || !$wbId || !$huobi) returnError("缺少参数");
        if (!$money) returnError("请输入正确的转换金额");
        if ($money > 0){
            $inc = db("huobi_zu_detail")->where("id",$rmbId)->value('bilv');
            $dec = db("huobi_zu_detail")->where("id",$wbId)->field('bilv,shouxu_fee')->find();
            $rmbBefore = db('jujian_bian')->where('yh_id', $this->id)->where('hb_id', 1)->value('zong_yongjin');
            $wbBefore = db('jujian_bian')->where('yh_id', $this->id)->where('hb_id', $huobi)->value('zong_yongjin');
            $oldMoney = $wbBefore;
        }else {
            $inc = db("huobi_zu_detail")->where("id",$wbId)->value('bilv');
            $dec = db("huobi_zu_detail")->where("id",$rmbId)->field('bilv,shouxu_fee')->find();
            $rmbBefore = db('jujian_bian')->where('yh_id', $this->id)->where('hb_id', 1)->value('zong_yongjin');
            $wbBefore = db('jujian_bian')->where('yh_id', $this->id)->where('hb_id', $huobi)->value('zong_yongjin');
            $oldMoney = $rmbBefore;
        }
        if ($oldMoney < abs($money)) returnError("余额不足");
        $incMoney = (abs($money)-$dec['shouxu_fee'])/$dec['bilv']*$inc;
        Db::startTrans();
        if ($money>0){
            $a = db('jujian_bian')
                ->where('yh_id', $this->id)
                ->where('hb_id', 1)
                ->setInc('zong_yongjin' , $incMoney);
            $b = db('jujian_bian')
                ->where('yh_id', $this->id)
                ->where('hb_id', $huobi)
                ->setDec('zong_yongjin' , $money);
            $rmbSave = $incMoney;
            $wbSave = -$money;
        }else {
            $a = db('jujian_bian')
                ->where('yh_id', $this->id)
                ->where('hb_id', 1)
                ->setDec('zong_yongjin' , abs($money));
            $b = db('jujian_bian')
                ->where('yh_id', $this->id)
                ->where('hb_id', $huobi)
                ->setInc('zong_yongjin' , $incMoney);
            $rmbSave = $money;
            $wbSave = $incMoney;
        }
        $rmbAfter = db('jujian_bian')->where('yh_id', $this->id)->where('hb_id', 1)->value('zong_yongjin');
        $wbAfter = db('jujian_bian')->where('yh_id', $this->id)->where('hb_id', $huobi)->value('zong_yongjin');
        $res['yh_id'] = $this->id;
        $res['lb'] = 9;
        $res['hb_id'] = 1;
        $res['jine'] = $rmbSave;
        $res['yue'] = $rmbAfter;
        $res['shijian'] = time();
        $res['zh_lb'] = 2;
        $c = db('jujian_liushui')->insert($res);
        $re['yh_id'] = $this->id;
        $re['lb'] = 10;
        $re['hb_id'] = $huobi;
        $re['jine'] = $wbSave;
        $re['yue'] = $wbAfter;
        $re['shijian'] = time();
        $re['zh_lb'] = 2;
        $d = db('jujian_liushui')->insert($re);
        if($a &&$b && $c && $d){
            // 执行提交操作
            Db::commit();
            $operation_category = 115;
            $detail = array(
                '货币转换'  => '成功',
                '转换金额'  => abs($money),
                '转换关系'  => ($money>0)?$huobi."->1":"1->".$huobi,
                '人民币转换前余额' => $rmbBefore,
                '人民币转换后余额' => $rmbAfter,
                '外币转换前余额' => $wbBefore,
                '外币转换后余额' => $wbAfter
            );
            $redis = ['update_bz'=>1];
            $this->riZhi($operation_category, $detail, $redis);
            returnSuccess(1);
        }else{
            // 回滚事务
            Db::rollback();
            // 获取提示信息
            returnError("插入数据错误，请重试！");
        }
    }

    // 获取当前人民币余额
    public function getMoney(){
        $power = db('jujian')->where('id', $this->id)->value("lb_id");
        if ($power != 2) returnError("非代理身份，无操作权限！");
        $huobi = input("id/d");
        $shuju = db('jujian_bian')
            ->alias('j')
            ->join('huobi h', 'j.hb_id = h.id', 'LEFT')
            ->join('jujian u', 'j.yh_id = u.id', 'LEFT')
            ->where('h.id', ($huobi)?$huobi:1)
            ->where('j.yh_id', $this->id)
            ->field('j.zong_yongjin,h.name as huobi_name,u.bh,u.xingming')
            ->find();
        $xuanze = db('huobi')->field('id,name')->select();
        returnSuccess([$shuju,$xuanze]);
    }

    // 确认提现
    public function putForward(){
        $power = db('jujian')->where('id', $this->id)->value("lb_id");
        if ($power != 2) returnError("非代理身份，无操作权限！");
        $huobi = input('id/d');
        $money = input('money/d');
        if (!$huobi) returnError("缺少参数HBID");
        if (!$money || $money<0) returnError("请正确填写金额");
        $cjSum = db('chujin')->where('yh_id', $this->id)->where('yh_lb', 2)->where('shenhe', 0)->sum('jine');
        if (!$cjSum){
            $cjSum = 0;
        }
        $zongMoney = db('jujian_bian')->alias('j')->join('huobi h', 'j.hb_id = h.id')->where('h.id',$huobi)->where('j.yh_id',$this->id)->value('j.zong_yongjin');
        if (($cjSum+$money) > $zongMoney) returnError("当前已申请金额$cjSum,可申请金额为".($zongMoney-$cjSum));
        $insert = array(
            'yh_id' => $this->id,
            'yh_lb' => 2,
            'zh_lb' => 2,
            'bank_id'=>db('jujian')->where('id',$this->id)->value('bank_id'),
            'jine'  => $money,
            'shijian'=>time(),
            'liushui_bh'=>strtoupper(substr($this->bh,0,2)).date("YmdHis").rand(1000,9999),
        );
        if (db('chujin')->insert($insert)){
            returnSuccess(1);
        }else {
            returnError("申请失败");
        }
    }


    /**
     * 出金审核查询
     */
    public function outMoneyQuery(){
        if(!$this->is_Dingji){
            returnError('无权查看！');
        }
        $id = $this->id;
        if($this->isCZY){
            $id = $this->jujian['par_id'];
        }
        $jujianData = db('chujin')
            ->alias('c')
            ->join('jujian y', 'c.yh_id = y.id', 'LEFT')
            ->field('c.id,c.jine,c.shijian,y.bh,y.xingming')
            ->whereLike('y.sj_xulie', "%," . $id . ",%")
            ->where('y.bz', 1)
            ->where('c.yh_lb',2)
            ->where('c.shenhe', 0)
            ->select();
        $yonghuData = db('chujin')
            ->alias('c')
            ->join('yonghu y', 'c.yh_id = y.id', 'LEFT')
            ->field('c.id,c.jine,c.shijian,y.bh,y.xingming')
            ->whereLike('y.sj_xulie', "%," . $id . ",%")
            ->where('y.bz', 1)
            ->where('c.yh_lb',1)
            ->where('c.shenhe', 0)
            ->select();
        $data = array_merge_recursive($jujianData,$yonghuData);
        if(!empty($data)){
            returnSuccess($data);
        }else{
            returnError('未查询到数据');
        }

    }

    /**
     * 出金审核提交money/outMoneySubmit
     * id
     */
    public function outMoneySubmit(){
        if(!$this->is_Dingji){
            returnError('无权限');
        }
        $id = $this->isCZY?$this->par_id:$this->id;
        $jujian = db('jujian')->where('id',$id)->find();
        if(!$jujian['yuming']){
            returnError('没有跳转域名，请检查！');
        }
        $chujin_id = input('id/d');
        $chujinData = db('chujin')->find($chujin_id);
        if ($chujinData){
            if ($chujinData['shenhe'] != 0) returnError("参数错误请刷新后重试！");
        }else{
            returnError("参数错误请刷新后重试！");
        }
        $suiji = ['A','B','C','D','E','F','G','H','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
        $pass = '';
        for ($i=0;$i<10;$i++){
            $pass .= $suiji[mt_rand(0,20)];
        }
        $no_or_yes = input('no_or_yes/d'); //1通过2删除
        if($no_or_yes == 1 || $no_or_yes == 2){
            if(config('is_shipan')){
                if (db('jujian')->where('id', $id)->update(['suiji_pass' => $pass])) {
                    $arr = ['yh_id'=>$id,'lb'=>$no_or_yes,'id'=>$chujin_id,'pass'=>$pass];
                    $redis = ['cjsh'=>1];
                    $this->riZhi(($no_or_yes==1)?20:21,$arr,$redis);
                    $url = "http://{$jujian['yuming']}:8080/churu/shenhe_tg.php?yh_id=$id&lb=$no_or_yes&id=$chujin_id&pass=$pass";// id出金表的id,yh_id当前登录的id,
                    returnSuccess($url);
                    die;
                }
            }else{
                returnSuccess('http://www.baidu.com');die;
            }
        }
        returnError('操作失败，请重试.！');
    }

    /**
     * 资金流水接口 money/moneyDetail
     * type(1客户或2代理商)
     * now(1现在或2两个月前)
     * hb_id(货币id)
     * name(账号或姓名或电话搜索)
     * startTime（开始时间）
     * endTime（结束时间）
     */
    public function moneyDetail(){
        $type = input('type/d',1);
        $now = input('now/d',1);
        $id = $this->id;
        if($this->isCZY){
            $id = $this->jujian['par_id'];
        }
        if($type == 1){
            $where['yonghu.sj_xulie'] = ['like',"%,$id,%"];
            if(input('hb_id/d')){
                $where['yl.hb_id'] = input('hb_id/d');
            }
            if(input('name')){
                $name=input('name');
                $where['yonghu.xingming|yonghu.bh|yonghu.tel'] =['like', "%$name%"];
            }
            $startTime = input('startTime',strtotime(date('Y-m-d',time())),'strtotime');
            $endTime= input('endTime');
            if(!empty($endTime)){
                $endTime= strtotime($endTime) + 3600*24;
            }else{
                $endTime = strtotime(date('Y-m-d',time()+3600*24));
            }
            if($now == 1){
                $time = time() - 60 * 24 * 3600;
                if($startTime < $time){
                    $startTime =$time ;
                }
            }
            $where['shijian'] = ['between time',[(int)$startTime,(int)$endTime]];
            $data = db('yonghu_liushui')
                ->alias('yl')
                ->field("bh,xingming,sj_xulie,lb,jine,yue,hb_id,shijian,oid")
                ->join('yonghu',"yonghu.id = yl.yh_id",'LEFT')
                ->where($where)
                ->order("yl.id desc")
                ->select();
            $data1 = db('yonghu_liushui_history')
                ->alias('yl')
                ->field("bh,xingming,sj_xulie,lb,jine,yue,hb_id,shijian,oid")
                ->join('yonghu',"yonghu.id = yonghu_liushui_history.yh_id",'LEFT')
                ->where($where)
                ->order("yl.id desc")
                ->select();
            $data = array_merge_recursive($data,$data1);
        }else{
            $or="(jujian.sj_xulie like '%,$id,%' or jujian.id = $id)";
            if(input('hb_id/d')){
                $where['jl.hb_id'] = input('hb_id/d');
            }
            if(input('name')){
                $where['jujian.xingming|jujian.bh|jujian.tel'] = input('name');
            }
            $startTime = input('startTime',strtotime(date('Y-m-d',time())),'strtotime');
            $endTime= input('endTime');
            if(!empty($endTime)){
                $endTime= strtotime($endTime) + 3600*24;
            }else{
                $endTime = strtotime(date('Y-m-d',time()+3600*24));
            }
            if($now == 1){
                $time = time() - 60 * 24 * 3600;
                if($startTime < $time){
                    $startTime =$time ;
                }
            }
            $where['jl.shijian'] = ['between time',[(int)$startTime,(int)$endTime]];
            $data = db('jujian_liushui')
                ->alias('jl')
                ->field("bh,xingming,sj_xulie,lb,jl.jine,yue,hb_id,shijian,oid,zh_lb")
                ->join('jujian',"jujian.id = jl.yh_id",'LEFT')
                ->join('liushui_lb',"liushui_lb.id =jl.lb ",'LEFT')
                ->join('huobi',"huobi.id =jl.hb_id")
                ->where($where)
                ->where($or)
                ->order("jl.shijian desc")
                ->select();

            $data1 = db('jujian_liushui_history')
                ->alias('jl')
                ->field("bh,xingming,sj_xulie,lb,jl.jine,yue,hb_id,shijian,oid,zh_lb")
                ->join('jujian',"jujian.id = jl.yh_id",'LEFT')
                ->join('liushui_lb',"liushui_lb.id =jl.lb ",'LEFT')
                ->join('huobi',"huobi.id =jl.hb_id")
                ->where($where)
                ->where($or)
                ->order("jl.shijian desc")
                ->select();
            $data = array_merge_recursive($data,$data1);

        }
        $liushui_lb = db('liushui_lb')->field('id,name')->select();
        $liushui_lb = $this->getNewArray($liushui_lb,'id');
        $huobi = db('huobi')->field('id,name')->select();
        $huobi = $this->getNewArray($huobi,'id');
        $zhlb =['0'=>'基本账户','1'=>'佣金账户','2'=>'有效佣金账户'];
        foreach ($data as $k=>$v){
            $data[$k]['lsdh'] = '';
            $data[$k]['yhls'] = '';
            $data[$k]['zt'] = '';
            $data[$k]['agents'] = $this->get_dl_name($v['sj_xulie']);
            unset( $data[$k]['sj_xulie']);
            if($data[$k]['lb'] == 1){
                $rujin = db('rujin')->where('id',$v['oid'])->cache(3600*24)->field('liushui_bh as lsdh,liushui_bh1 as yhls,bz')->find();
                $data[$k]['lsdh'] = $rujin['lsdh'];
                $data[$k]['yhls'] = $rujin['yhls'] ? $rujin['yhls'] : '';
                $data[$k]['yhls'] = $rujin['bz'] == 1 ? '成功' : '支付中';
            }elseif ($data[$k]['lb'] == 2){
                $chujin = db('chujin')->where('id',$v['oid'])->cache(3600*24)->field('liushui_bh as lsdh,liushui_bh1 as yhls,bz')->find();
                $data[$k]['lsdh'] = $chujin['lsdh'];
                $data[$k]['yhls'] = $chujin['yhls'] ? $chujin['yhls'] : '';
                $data[$k]['yhls'] = $chujin['bz'] == 1 ? '成功' : '支付中';
            }
            $data[$k]['lb'] = $liushui_lb[$data[$k]['lb']]['name'];
            $data[$k]['hb_id'] = $huobi[$data[$k]['hb_id']]['name'];
//            $data[$k]['shijian'] = date('Y-m-d H:i',$data[$k]['shijian']);
            if(isset( $data[$k]['zh_lb'])){
                $data[$k]['zh_lb'] =  $zhlb[$data[$k]['zh_lb']];
            }
            if(config('web_name') == 'HC'){
                if(!$this->is_Dingji){
                    if(preg_match("/^1[345678]{1}\d{9}$/", $data[$k]['bh'])){
                        $data[$k]['bh'] = substr_replace( $data[$k]['bh'],'****',3,4);
                    }
                }
            }
        }
        if(!empty($data)){
            returnSuccess($data);
        }else{
            returnError('未查询到数据');
        }
    }

    /**
    =======
    /**
     * 出金详情
     */
    public function outMoneyDetail(){
        $type = input('type/d',1);
        $id = $this->id;
        if($this->isCZY){
            $id = $this->jujian['par_id'];
        }
        $where['chujin.yh_lb'] = $type;
        if(input('name')){
            $name=input('name');
            $where['xingming|bh|tel'] =['like', "%$name%"];
        }
        $startTime = input('startTime',strtotime(date('Y-m-d',time())),'strtotime');
        $endTime= input('endTime');
        if(!empty($endTime)){
            $endTime= strtotime($endTime) + 3600*24;
        }else{
            $endTime = strtotime(date('Y-m-d',time()+3600*24));
        }
        $where['shijian'] = ['between time',[(int)$startTime,(int)$endTime]];
        if($type ==1){
            $where['sj_xulie'] = ['like',"%,$id,%"];
            $data = db('chujin')
                ->field('sj_xulie,bh,xingming,chujin.bank_id,liushui_id,liushui_bh,liushui_bh1,chujin.jine,chujin.bz,zh_lb,shijian,miaoshu,shenhe')
                ->join('yonghu','chujin.yh_id = yonghu.id','LEFT')
                ->where($where)
                ->select();
        }else{
            $or="(jujian.sj_xulie like '%,$id,%' or jujian.id = $id)";
            $data = db('chujin')
                ->field('sj_xulie,bh,xingming,chujin.bank_id,liushui_id,liushui_bh,liushui_bh1,chujin.jine,chujin.bz,zh_lb,shijian,miaoshu,shenhe')
                ->join('jujian','chujin.yh_id = jujian.id','LEFT')
                ->where($where)
                ->where($or)
                ->select();
        }
        $zhlb =['0'=>'基本账户','1'=>'佣金账户','2'=>'有效佣金账户'];
        $bank = db('bank')->field('id,name')->select();
        $bank = $this->getNewArray($bank,'id');
        foreach ($data as $k=>$v){
            $data[$k]['agents'] = $this->get_dl_name($v['sj_xulie']);
            unset( $data[$k]['sj_xulie']);
            $data[$k]['bank_id'] = $bank[$data[$k]['bank_id']]['name'];
            $data[$k]['bz'] =  $data[$k]['bz'] == 1 ? '成功' : '失败';
            $data[$k]['zh_lb'] =  $zhlb[$data[$k]['zh_lb']];
//            $data[$k]['shijian'] = date('Y-m-d H:i',$data[$k]['shijian']);
            if(config('web_name') == 'HC'){
                if(!$this->is_Dingji){
                    if(preg_match("/^1[345678]{1}\d{9}$/", $data[$k]['bh'])){
                        $data[$k]['bh'] = substr_replace( $data[$k]['bh'],'****',3,4);
                    }
                }
            }
        }
        if(!empty($data)){
            returnSuccess($data);
        }else{
            returnError('未查询到数据');
        }
    }

    public function inMoneyDetail(){
        $type = input('type/d',1);
        $id = $this->id;
        if($this->isCZY){
            $id = $this->jujian['par_id'];
        }
        $where['rujin.yh_lb'] = $type;
        if(input('name')){
            $name=input('name');
            $where['xingming|bh|tel'] = ['like', "%$name%"];
        }
        $startTime = input('startTime',strtotime(date('Y-m-d',time())),'strtotime');
        $endTime= input('endTime');
        if(!empty($endTime)){
            $endTime= strtotime($endTime) + 3600*24;
        }else{
            $endTime = strtotime(date('Y-m-d',time()+3600*24));
        }
        $where['shijian'] = ['between time',[(int)$startTime,(int)$endTime]];
        if($type ==1){
            $where['sj_xulie'] = ['like',"%,$id,%"];
            $data = db('rujin')
                ->field('sj_xulie,bh,xingming,rujin.bank_id,liushui_bh,liushui_bh1,rujin.jine,rujin.bz,shijian')
                ->join('yonghu','rujin.yh_id = yonghu.id','LEFT')
                ->where($where)
                ->select();
        }else{
            $or="(jujian.sj_xulie like '%,$id,%' or jujian.id = $id)";
            $data = db('rujin')
                ->field('sj_xulie,bh,xingming,rujin.bank_id,liushui_bh,liushui_bh1,rujin.jine,rujin.bz,shijian')
                ->join('jujian','rujin.yh_id = jujian.id','LEFT')
                ->where($where)
                ->where($or)
                ->select();
        }
        $zhlb =['0'=>'基本账户','1'=>'佣金账户','2'=>'有效佣金账户'];
        $bank = db('bank')->field('id,name')->select();
        $bank = $this->getNewArray($bank,'id');
        foreach ($data as $k=>$v){
            $data[$k]['agents'] = $this->get_dl_name($v['sj_xulie']);
            unset( $data[$k]['sj_xulie']);
            $data[$k]['bank_id'] = @$bank[$data[$k]['bank_id']]['name'];
            $data[$k]['bz'] =  $data[$k]['bz'] == 1 ? '成功' : '失败';
//            $data[$k]['shijian'] = date('Y-m-d H:i',$data[$k]['shijian']);
            if(config('web_name') == 'HC'){
                if(!$this->is_Dingji){
                    if(preg_match("/^1[345678]{1}\d{9}$/", $data[$k]['bh'])){
                        $data[$k]['bh'] = substr_replace( $data[$k]['bh'],'****',3,4);
                    }
                }
            }
        }
        if(!empty($data)){
            returnSuccess($data);
        }else{
            returnError('未查询到数据');
        }
    }




    /**
     * 分页版资金流水接口
     * money/newMoneyDetail
     * type(1客户或2代理商)
     * now(1现在或2两个月前)
     * hb_id(货币id)
     * name(账号或姓名或电话搜索)
     * startTime（开始时间）
     * endTime（结束时间）
     */
    public function newMoneyDetail(){
        $type = input('type/d',1);
        $now = input('now/d',1);
        $tiaojian=input('sj_value');
        $class=input('lb_value');
        $bizhong=input('money_value');
        $paixu=input('px_value/d');
        $id = $this->id;
        if($this->isCZY){
            $id = $this->jujian['par_id'];
        }
        $page = input('page/d',1);
        $limit = input('limit/d',20);
        //type=1代表终端客户
        if($type == 1){
            if($now == 1){
                $db = 'yonghu_liushui';
            }else{
                $db = 'yonghu_liushui_history';
            }
            //排序
            switch ($paixu) {
                case 1:
                    $order="$db.jine asc";
                    break;
                case 2:
                    $order="$db.jine desc";
                    break;
                case 3:
                    $order="$db.yue asc";
                    break;
                case 4:
                    $order="$db.yue desc";
                    break;
                case 5:
                    $order="$db.shijian asc";
                    break;
                case 6:
                    $order="$db.desc asc";
                    break;
                default:
                    $order="$db.id desc";
            }
            $or='';
            $or_class='';
            $bz_class='';
            //代理条件筛选
            if(!empty($tiaojian)){
                $sonJujianId=array_filter(explode(',',$tiaojian));
                if(count($sonJujianId) >=1){
                    if(count($sonJujianId) ==1) {
                        //判断选择的是不是自己
                        if ($sonJujianId[0] == $this->id) {
                            $where['yonghu.jujian_id'] = $sonJujianId[0];
                        }else{
                            $where['yonghu.sj_xulie'] = ['like', "%,$sonJujianId[0],%"];
                        }
                    }else {
                        foreach ($sonJujianId as $k => $v) {
                            if(is_numeric($v) && $v>0){
                                if($or == ''){
                                    $or .= " (yonghu.sj_xulie like '%,$v,%'";
                                }else{
                                    $or .= " or yonghu.sj_xulie like '%,$v,%'";
                                }
                            }
                        }
                        if($or != ''){
                            $or .= ')';
                        }
                    }
                }
            }else{
                $where['yonghu.sj_xulie'] = ['like',"%,$id,%"];
            }
            //类型筛选
            if(!empty($class)){
                $lb_array=array_filter(explode(',',$class));
                foreach($lb_array as $k1=>$v1){
                    if($or_class==''){
                        $or_class.='('.$db.'.'."lb=$v1";
                    }else{
                        $or_class.=" or $db.lb=$v1";;
                    }
                }
                if($or_class != ''){
                    $or_class .= ')';
                }
            }
            //币种筛选
            if(!empty($bizhong)){
                $bizhong_array=array_filter(explode(',',$bizhong));
                foreach($bizhong_array as $k2=>$v2){
                    if($bz_class==''){
                        $bz_class.='('.$db.'.'."hb_id=$v2";
                    }else{
                        $bz_class.=" or $db.hb_id=$v2";;
                    }
                }
                if($bz_class != ''){
                    $bz_class .= ')';
                }
            }
            if(input('hb_id/d')){
                $where[$db.'.hb_id'] = input('hb_id/d');
            }
//            else{
//                $where[$db . '.hb_id'] = input('hb_id/d');
//            }
            if(input('name')){
                $name=input('name');
                $where['yonghu.xingming|yonghu.bh|yonghu.tel'] =['like', "%$name%"];
            }
            $startTime = input('startTime',strtotime(date('Y-m-d',time())),'strtotime');
            $endTime= input('endTime');
            if(!empty($endTime)){
                $endTime= strtotime($endTime) + 3600*24;
            }else{
                $endTime = strtotime(date('Y-m-d',time()+3600*24));
            }
            $where['shijian'] = ['between time',[(int)$startTime,(int)$endTime]];
            //获取筛选的条件
            $sj_xulie = db($db)
                ->field("substring(substring_index(`sj_xulie`,',',2),2) as value,sj_xulie as text,sum(jine) as jine,sum(yue) as yue")
                ->join('yonghu',"yonghu.id = $db.yh_id",'LEFT')
                ->where($where)
                ->where($or)
                ->where($or_class)
                ->where($bz_class)
                ->group('sj_xulie')
                ->order('yonghu.id asc')
                ->select();
            $sumJine = $sumYue = 0;
            foreach($sj_xulie as $k=>$v){
                $sj_xulie[$k]['text'] = $this->get_dl_name($v['text']);
                $sumJine += $v['jine'];
                $sumYue += $v['yue'];
                unset($sj_xulie[$k]['jine']);
                unset($sj_xulie[$k]['yue']);
            }
            $lb = db($db)
                ->field("lb as value,liushui_lb.name")
                ->join('yonghu',"yonghu.id = $db.yh_id",'LEFT')
                ->join('liushui_lb',"liushui_lb.id = $db.lb",'LEFT')
                ->where($where)
                ->group('lb')
                ->select();
            $hb = db($db)
                ->field("hb_id as value,huobi.name as text")
                ->join('yonghu',"yonghu.id = $db.yh_id",'LEFT')
                ->join('huobi',"huobi.id = $db.hb_id",'LEFT')
                ->where($where)
                ->group('hb_id')
                ->select();
            $num = db($db)
                ->join('yonghu',"yonghu.id = $db.yh_id",'LEFT')
                ->where($where)
                ->where($or)
                ->where($or_class)
                ->where($bz_class)
                ->order($order)
                ->page($page,$limit)
                ->count();
            $data = db($db)
                ->field("bh,xingming,sj_xulie,lb,jine,yue,hb_id,shijian,oid")
                ->join('yonghu',"yonghu.id = $db.yh_id",'LEFT')
                ->where($where)
                ->where($or)
                ->where($or_class)
                ->where($bz_class)
                ->order($order)
                ->page($page,$limit)
                ->select();
        }else{
            //代理商
            if($now == 1){
                $db = 'jujian_liushui';
            }else{
                $db = 'jujian_liushui_history';
            }
            //排序
            switch ($paixu) {
                case 1:
                    $order="$db.jine asc";
                    break;
                case 2:
                    $order="$db.jine desc";
                    break;
                case 3:
                    $order="$db.yue asc";
                    break;
                case 4:
                    $order="$db.yue desc";
                    break;
                case 5:
                    $order="$db.shijian asc";
                    break;
                case 6:
                    $order="$db.desc asc";
                    break;
                default:
                    $order="$db.shijian desc";
            }
            //关系
            $or = '';
            $or_class='';
            $bz_class='';
            if (!empty($tiaojian)) {
                $tiaojian = array_filter(explode(',', $tiaojian));
                if (count($tiaojian) >= 1) {
                    if (count($tiaojian) == 1) {
                        //判断选择的是不是自己
                        if ($tiaojian[0] == $this->id) {
                            $where['jujian.id'] = $tiaojian[0];
                        } else {
                            $where['jujian.sj_xulie'] = ['like', "%,$tiaojian[0],%"];
                        }
                    } else {
                        foreach ($tiaojian as $k1 => $v1) {
                            if ($k1 == 0) {
                                $or .= "(jujian.sj_xulie like '%,$v1,%'";
                            } else {
                                $or .= " or jujian.sj_xulie like '%,$v1,%' or jujian.id = '$id'";
                            }
                        }
                        if($or != ''){
                            $or .= ')';
                        }
                    }
                }
            } else {
                $or .= "jujian.sj_xulie like '%,$id,%'  or jujian.id = '$id'";
            }
            //类型筛选
            if(!empty($class)){
                $lb_array=array_filter(explode(',',$class));
                foreach($lb_array as $k1=>$v1){
                    if($or_class==''){
                        $or_class.='('.$db.'.'."lb=$v1";
                    }else{
                        $or_class.=" or $db.lb=$v1";;
                    }
                }
                if($or_class != ''){
                    $or_class .= ')';
                }
            }
            //币种筛选
            if(!empty($bizhong)){
                $bizhong_array=array_filter(explode(',',$bizhong));
                foreach($bizhong_array as $k2=>$v2){
                    if($bz_class==''){
                        $bz_class.='('.$db.'.'."hb_id=$v2";
                    }else{
                        $bz_class.=" or $db.hb_id=$v2";;
                    }
                }
                if($bz_class != ''){
                    $bz_class .= ')';
                }
            }
            if(input('hb_id/d')){
                $where[$db.'.hb_id'] = input('hb_id/d');
            }
            if(input('name')){
                $where['jujian.xingming|jujian.bh|jujian.tel'] = input('name');
            }
            $startTime = input('startTime',strtotime(date('Y-m-d',time())),'strtotime');
            $endTime= input('endTime');
            if(!empty($endTime)){
                $endTime= strtotime($endTime) + 3600*24;
            }else{
                $endTime = strtotime(date('Y-m-d',time()+3600*24));
            }
            $where[$db.'.shijian'] = ['between time',[(int)$startTime,(int)$endTime]];
            //获取筛选的条件
            $sj_xulie = db($db)
                ->field("substring(substring_index(`sj_xulie`,',',2),2) as value,sj_xulie as text,sum(jine) as jine,sum(yue) as yue")
                ->join('jujian',"jujian.id = $db.yh_id",'LEFT')
                ->where($where)
                ->group('sj_xulie')
                ->order('jujian.id asc')
                ->select();
            $sumJine = $sumYue = 0;
            foreach($sj_xulie as $k=>$v){
                $sj_xulie[$k]['text'] = $this->get_dl_name($v['text']);
                $sumJine += $v['jine'];
                $sumYue += $v['yue'];
                unset($sj_xulie[$k]['jine']);
                unset($sj_xulie[$k]['yue']);
            }
            $lb = db($db)
                ->field("lb as value,liushui_lb.name")
                ->join('jujian',"jujian.id = $db.yh_id",'LEFT')
                ->join('liushui_lb',"liushui_lb.id = $db.lb",'LEFT')
                ->where($where)
                ->group('lb')
                ->select();
            $hb = db($db)
                ->field("hb_id as value,huobi.name as text")
                ->join('jujian',"jujian.id = $db.yh_id",'LEFT')
                ->join('huobi',"huobi.id = $db.hb_id",'LEFT')
                ->where($where)
                ->group('hb_id')
                ->select();
            $num = db($db)
                ->join('jujian',"jujian.id = $db.yh_id",'LEFT')
                ->join('liushui_lb',"liushui_lb.id =$db.lb ",'LEFT')
                ->join('huobi',"huobi.id =$db.hb_id")
                ->where($where)
                ->where($or)
                ->where($or_class)
                ->where($bz_class)
                ->order($order)
                ->count();
            $data = db($db)
                ->field("bh,xingming,sj_xulie,lb,$db.jine,yue,hb_id,shijian,oid,zh_lb")
                ->join('jujian',"jujian.id = $db.yh_id",'LEFT')
                ->join('liushui_lb',"liushui_lb.id =$db.lb ",'LEFT')
                ->join('huobi',"huobi.id =$db.hb_id")
                ->where($where)
                ->where($or)
                ->where($or_class)
                ->where($bz_class)
                ->page($page,$limit)
                ->order($order)
                ->select();
        }

        $liushui_lb = db('liushui_lb')->field('id,name')->select();
        $liushui_lb = $this->getNewArray($liushui_lb,'id');
        $huobi = db('huobi')->field('id,name')->select();
        $huobi = $this->getNewArray($huobi,'id');
        $zhlb =['0'=>'基本账户','1'=>'佣金账户','2'=>'有效佣金账户'];
        foreach ($data as $k=>$v){
            $data[$k]['lsdh'] = '';
            $data[$k]['yhls'] = '';
            $data[$k]['zt'] = '';
            $data[$k]['agents'] = $this->get_dl_name($v['sj_xulie']);
            unset( $data[$k]['sj_xulie']);
            if($data[$k]['lb'] == 1){
                $rujin = db('rujin')->where('id',$v['oid'])->field('liushui_bh as lsdh,liushui_bh1 as yhls,bz')->find();
                $data[$k]['lsdh'] = $rujin['lsdh'];
                $data[$k]['yhls'] = $rujin['yhls'] ? $rujin['yhls'] : '';
                $data[$k]['yhls'] = $rujin['bz'] == 1 ? '成功' : '支付中';
            }elseif ($data[$k]['lb'] == 2){
                $chujin = db('chujin')->where('id',$v['oid'])->field('liushui_bh as lsdh,liushui_bh1 as yhls,bz')->find();
                $data[$k]['lsdh'] = $chujin['lsdh'];
                $data[$k]['yhls'] = $chujin['yhls'] ? $chujin['yhls'] : '';
                $data[$k]['yhls'] = $chujin['bz'] == 1 ? '成功' : '支付中';
            }
            $data[$k]['lb'] = $liushui_lb[$data[$k]['lb']]['name'];
            $data[$k]['hb_id'] = $huobi[$data[$k]['hb_id']]['name'];
            if(isset( $data[$k]['zh_lb'])){
                $data[$k]['zh_lb'] =  $zhlb[$data[$k]['zh_lb']];
            }
        }

        if(!empty($data)){
            $data['agents'] = $sj_xulie;
            $data['hb'] = $hb;
            $data['lb'] = $lb;
            $data['sumJine'] = $sumJine;
            $data['sumYue'] = $sumYue;
            $data['num'] = $num;
            returnSuccess($data);
        }else{
            returnError('未查询到数据');
        }
    }








}